$(document).ready(function() {

	var itemno = getUrlParam("itemno");
	binFormData(itemno);

	
	$('.summernote').summernote({	//实例化summernote控件
		height : 200,
		tabsize : 2,
		lang : 'zh-CN'
	});
});

function initTable(itemno, fileid){
	var data1 = {"fileid": fileid};
	
	$('#tableid').attr('fileid', fileid);

	$.ajax({
		method : 'post',
		url : projectPath + "declare/guidetable.do",
		data : JSON.stringify(data1),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
			var params = {"busscode":'06'};
			var fileData = null;
			if(data.result == 1){
				fileData = data.datas;
			}
			
		    initFileInput("tableid", params, fileData);
		}
	});
}

function initStyleText(itemno, fileid){
	var data1 = {"fileid": fileid};

	$('#styletext').attr('fileid', fileid);

	$.ajax({
		method : 'post',
		url : projectPath + "declare/guidestyle.do",
		data : JSON.stringify(data1),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
			var params = {"busscode":'06'};
			var fileData = null;
			if(data.result == 1){
				fileData = data.datas;
			}
			
		    initFileInput("styletext", params, fileData);
		}
	});
}

var sgid = '';
function binFormData(itemno){
	var data = parent.getData(itemno);
	var itemname = data["itemname"];
	sgid = data["guideid"];
	
	$('[name="itemno"]').text(itemno);
	$('[name="itemname"]').text(itemname);

	if(sgid != ''){

		$.ajax({
			url : projectPath + "declare/getguideinfo.do",
			data : {"sgid":sgid},
			dataType : "json",
			contentType : "application/json",
			success : function(data) {
				var tableid = "";
				var styletext = "";
				if(data.result == 1) {
					var guide = data.datas;
	
					var d = formatDate(guide.publishdate, 'yyyy-mm-dd');
					$('[name="publishdate"]').val(d);
					$('[name="handleurl"]').val(guide.handleurl);
					$('[name="content"]').summernote('code', guide.content);
					$('#tableid').attr('fileid', guide.tableid);
					$('#styletext').attr('fileid', guide.styletext);
	
					tableid = guide.tableid;
					styletext = guide.styletext;
				}
				
				initTable(itemno, tableid);
				initStyleText(itemno, styletext);
			}
		});
	}
	else{
		var params = {"busscode":'06'};
	    initFileInput("tableid", params, null);
	    initFileInput("styletext", params, null);
	}
}

function save(){
	var data = {};
	data["itemno"] = $('[name="itemno"]').text();
	data["itemname"] = $('[name="itemname"]').text();
	data["publishdate"] = $('[name="publishdate"]').val();
	data["handleurl"] = $('[name="handleurl"]').val();
	data["content"] = $('[name="content"]').val();
	data["tableid"] = $('#tableid').attr('fileid');
	data["styletext"] = $('#styletext').attr('fileid');
	data["sgid"] = sgid;
	

	$.ajax({
		method : 'post',
		url : projectPath + "declare/saveguide.do",
		data : JSON.stringify(data),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
	    	window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success,{
				onOk:function(v){
					if(data.result == 1){
			    		parent.refreshData();		//刷新数据
			    	} 
				}
			});	
		}
	});
}
